﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using Jumpton.SFA.Server.BusinessRule;
using Jumpton.SFA.Server.BusinessRule.GIS;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.Persistent;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule.CallRoute;
using System.Net;
using Jumpton.SFA.Server.BusinessRule.MasterData;

public partial class Traffic_DELIVERY_GPS : PageBase, ICallbackEventHandler
{
    GISManager gisManager = new GISManager();
    StateManager stateManager = new StateManager();
    OrgainzationManager orgManager = new OrgainzationManager();
    public string _content;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
            BindUser();
            if (txtHidOrg.Value == "")
            {
                InitForm();
                DataBind();
            } 
        } 
    }

    private void BindUser()
    {
        ddlUserList.Items.Clear();

        if (!string.IsNullOrEmpty(txtHidUsers.Value))
        {
            string[] options = txtHidUsers.Value.Split('∮');

            for (int i = 0; i < options.Length; i++)
            {
                string[] item = options[i].Split('∞');
                ddlUserList.Items.Add(new ListItem(item[1], item[0]));
            }
        }
        ddlUserList.Items.Insert(0, new ListItem("--请选择--", ""));
    }

    private void InitForm()
    {
        txtHidOrg.Value = OrgHidValue;
        txtOrg.Text = OrgShowValue;
        var data = new OrgainzationManager().SearchOrgTree(MyStateManager.DomainID, MyStateManager.OrgID, null);
        if (data.Rows.Count != 1 || new Jumpton.SFA.Server.BusinessRule.System.UserManager().isManager(MyStateManager.PersonID))
        {
            OrgHelper.BindUsersDDL(MyStateManager.DomainID, this.txtHidOrg.Value, this.ddlUserList);
        }
        else
        {
            Person person = new PersonManager().Load(MyStateManager.PersonID);
            ddlUserList.Items.Clear();
            ddlUserList.Items.Add(new ListItem(person.Name, MyStateManager.UserID.ToString()));
        }
        //OrgHelper.BindUsersDDL(this.MyStateManager.DomainID, txtHidOrg.Value, ddlUserList);

        ddlUserType.DataSource = new DictionaryItemsManager().GetItemList(DictionaryEnum.Represent, MyStateManager.DomainID);
        ddlUserType.DataTextField = "Name";
        ddlUserType.DataValueField = "DictionaryItemID";
        ddlUserType.DataBind();
        ddlUserType.Items.Insert(0, new ListItem("--所有--", ""));
    }


    public void RaiseCallbackEvent(string the_string)
    {
        _content = the_string;
    }

    public string GetCallbackResult()
    {
        //  拆分由客户端传过来的参数
        string[] parts = _content.Split('|');

        //  生成ServerMethod的参数队列
        object[] theArgs = new object[parts.Length - 1];
        for (int int_index = 1; int_index < parts.Length; int_index++)
            theArgs[int_index - 1] = parts[int_index];

        //  调用ServerMethod
        return (string)GetType().GetMethod(parts[0]).Invoke(this, theArgs);
    }

    public string UpdateVisits(string args)
    {
        string[] theArgs = args.Split('#');

        DataTable dtLocation = gisManager.GetVisitLocation(theArgs[0], DateTime.Parse(theArgs[1]));
        string jsUsers = "";
        if (dtLocation.Rows.Count > 0)
        {
            jsUsers = AjaxFactory.DataTableJson(dtLocation);

        }
        return jsUsers;
    }

    protected void ddlUserType_SelectedIndexChanged(object sender, EventArgs e)
    {
        System.Text.StringBuilder sb = new StringBuilder();

        if (string.IsNullOrEmpty(OrgHidValue))
        {
            return;
        }

        sb.AppendFormat(" MOBILE_USERS.DOMAIN_ID='{0}' ", MyStateManager.DomainID);

        sb.AppendFormat(" AND USERS.ORG_ID IN ({0}) ", OrgHidValue);

        if (!string.IsNullOrEmpty(ddlUserType.SelectedValue))
        {
            sb.AppendFormat(" AND DictionaryItems.DictionaryItemID ={0} ", ddlUserType.SelectedValue);
        }

        DataSet ds = MobileUserManager.SelectUserDDLwithType(sb.ToString());
        if (ds != null & ds.Tables.Count > 0)
        {
            ddlUserList.DataSource = ds.Tables[0];
            ddlUserList.DataTextField = "CODENAME";
            ddlUserList.DataValueField = "ID";
            ddlUserList.DataBind();
            ddlUserList.Items.Insert(0, new ListItem("--请选择--", ""));
        }
    }

    protected void txtOrg_TextChanged(object sender, EventArgs e)
    {
        BindUser();
    }

    

}
